package tongcheng.com.tongcheng.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.yanzhenjie.nohttp.FileBinary;
import com.yanzhenjie.nohttp.RequestMethod;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.adapter.ShigongAdapter;
import tongcheng.com.tongcheng.bean.StatusBean;
import tongcheng.com.tongcheng.bean.UpImageBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.widght.MessageDialog;

import static tongcheng.com.tongcheng.base.AllOrderDetialActivity.xiangInstance;

public class StartShigongActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.back_text)
    TextView mBackText;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.title_bar)
    RelativeLayout mBg;
    @Bind(R.id.shigong_ll1)
    LinearLayout mShigongLl1;
    @Bind(R.id.shigong_ok)
    Button mShigongOk;

    @Bind(R.id.shigong_rc)
    RecyclerView mShigongRc;

    @Bind(R.id.shigong_check)
    CheckBox mShigongCheck;
    private List<LocalMedia> selectList;
    private List<String> filepath=new ArrayList<>();
    private ShigongAdapter mSendOrderAdapter;
    private String mScene_pic="";
    private int mType;
    private int mId;
    private int mIs_bills;
    private StringBuffer mSb;
    private MessageDialog mMessageDialog;
    int tt = 0;

    @Override
    public Integer initResid() {
        return R.layout.activity_start_shigong;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {
        mMessageDialog = new MessageDialog(this);
        mMessageDialog.setOkClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mMessageDialog.dismiss();
                if(tt == 1){
                    if(xiangInstance != null){
                        xiangInstance.finish();
                    }
                }

                finish();
            }
        });
        /*mScene_pic = getIntent().getStringExtra("scene_pic");*/
        mType = getIntent().getIntExtra("type", -1);
        mId = getIntent().getIntExtra("id", -1);
       // mIs_bills = getIntent().getIntExtra("is_bills", -1);
        settitlebar(mBg);
        mTitle.setText("开始施工");
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        mShigongRc.setLayoutManager(gridLayoutManager);
        mSendOrderAdapter = new ShigongAdapter(filepath,this,this,mBg);
        mShigongRc.setAdapter(mSendOrderAdapter);

        mShigongOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UpShigongInfo();
            }
        });
    }
    //开始施工
    private void UpShigongInfo() {
        mScene_pic = mSb.toString();
        MyRequest request = new MyRequest(ConStans.OrderDetialButton_Url, RequestMethod.POST);
        request.add("type", mType);
        request.add("id", mId);
        request.add("scene_pic", mScene_pic);
        request.add("is_bills",mShigongCheck.isChecked()?1:0);
        CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                StatusBean statusBean = GsonUtil.GsonToBean(json, StatusBean.class);
                if (statusBean != null) {
                    if (statusBean.status == 1) {
                        tt = 1;
                        mMessageDialog.setTitle("提交成功").setMessage("请移至订单中心查看订单进度").setIcon(R.mipmap.tijiao).showit();
                    } else {
                        mMessageDialog.setTitle("提交失败").setMessage(statusBean.msg+"").setIcon(R.mipmap.quxiao).showit();
                    }
                }
            }
        });
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    // ShowToast.showToast("我被回调了");
//                    for (LocalMedia localMedia : selectList) {
//                        Log.e("tag", localMedia.getPath());
//                        filepath.add(localMedia.getPath());
//                        mSendOrderAdapter.notifyDataSetChanged();
//                    }

                    UpImage(selectList);

                    break;
            }
        }
    }



    //上传图片
    private void UpImage(List<LocalMedia> selectList) {
        MyRequest request = new MyRequest(ConStans.UpImage_Url, RequestMethod.POST);
        request.add("code", "image");
        request.add("num", selectList.size());
        Log.e("size", selectList.size()+"");
        for (int i = 0; i < selectList.size(); i++) {
            request.add("image" + (i+1), new FileBinary(new File(selectList.get(i).getPath())));
        }
        mSb = new StringBuffer();
        // request.add("picture", new FileBinary(new File(selectList.get(0).getPath())));
        CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                UpImageBean upImageBean = GsonUtil.GsonToBean(json, UpImageBean.class);
                if (upImageBean != null) {
                    List<UpImageBean.DataBean> data = upImageBean.data;
                    if (data != null) {
                        for (int i = 0; i < data.size(); i++) {
                            filepath.add(ConStans.BaseUrl_Test_url+data.get(i).filename);
                            mSb.append(data.get(i).filename + ",");
                        }

                        mSendOrderAdapter.notifyDataSetChanged();
                    }
                }
            }
        });

    }
}
